{layout name="Index/layout" /}<section class="ui-page ui-body-c ui-page-active" data-url="page1" id="page1" data-role="page">
    <header role="banner" class="ui-bar-a ui-header" data-role="header">
        <h1 aria-level="1" role="heading" tabindex="0" class="ui-title">GPS实时上报</h1>
    </header>
    <div role="main" data-role="content" class="ui-content">
        <input id="mysite" type="button" value="开始上报我的位置" onclick="startgps()"/>
        <input id="endgps" type="button" value="停止上报" onclick="endgps()"/>
    </div>
    <footer role="contentinfo" class="ui-bar-a ui-footer" data-role="footer">
        <h1 aria-level="1" role="heading" tabindex="0" class="ui-title">Kuddy.cn</h1>
    </footer>
</section>
<script type="text/javascript">
    window.num = 1;
    function startgps() {
        var gps = navigator.geolocation;
        if (gps) {
            gps.getCurrentPosition(showgps,
                    function (error) {
                        alert("接口错误, 错误代码: " + error.code + " 错误信息: " + error.message);
                    },
                    {maximumAge: 10000}); // 这里设置超时为10000毫秒，即10秒
        }
        else {
            showgps();
        }
    }

    function showgps(position) {
        function onSuccess($data, $status) {
            window.report = window.setTimeout(startgps,5000);
        }
        if (position) {
            var latitude = position.coords.latitude;
            var longitude = position.coords.longitude;
            $.ajax({
                type: "POST",
                url: "gps",
                cache: false,
                data: {
                    'latitude': latitude,
                    'longitude': longitude
                },
                success: onSuccess
            });
            window.mysite = $('#mysite');
            mysite.prop('disabled',true).addClass("ui-disabled");
            $('#endgps').prop('disabled',false).removeClass("ui-disabled").button('refresh');
            mysite.val('正在上报定位,第'+num+'次').button('refresh');
            num++;
        }
        else
            alert("没有获取到定位");
    }

    function endgps() {
        clearTimeout(window.report);
        mysite.prop('disabled',false).removeClass("ui-disabled");
        mysite.val('开始上报我的位置').button('refresh');
        $('#endgps').prop('disabled',true).addClass("ui-disabled").button('refresh');
        num = 1;
    }
    $('#endgps').prop('disabled',true).addClass("ui-disabled");
</script>